<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 2017/9/5
 * Time: 15:31
 */

namespace app\admin\controller;


use think\Controller;
use think\Request;
use \Auth;


class Common extends  Controller
{

    /**
     * 构造函数
     */
    public function __construct() {

        parent::__construct();
        $user_info = session('user_info');

        if (!$user_info['id']) {
            $this->redirect('Login/index');
        }


        //$name = CONTROLLER_NAME . '/' . ACTION_NAME;
        $controllerName = $this->request->controller();
        $actionName = $this->request->action();
        $name = $controllerName.'/'.$actionName;
        $not_arr = array('User/userinfo','Menu/submenu','Index/index','Index/main');

        if (!in_array($name,$not_arr) && session('user_info')['id'] != 1) {

            $auth = new Auth();
            $auth_result = $auth->check($name, $user_info['id']);

            if ($auth_result === false) {
                if (Request::instance()->isAjax()) {
                    $this->ajaxError('没有权限!');
                } else {
                    $this->ajaxError('没有权限!');
                }

            }
        }

    }

    /**
     * @description:错误返回
     * @param string $msg
     * @param unknown $fields
     */
    protected function ajaxError($msg = '', $fields = array()) {

        header('Content-Type:application/json; charset=utf-8');
        $data = array('status' => 'error', 'msg' => $msg, 'fields' => $fields);
        echo json_encode($data);
        exit;
    }

    protected function ajaxSuccess($msg, $_data = array()) {

        header('Content-Type:application/json; charset=utf-8');
        $data = array('status' => 'success', 'msg' => $msg, 'data' => $_data);
        echo json_encode($data);
        exit;
    }

}